#!/usr/bin/perl
# download_log.cgi
# Download logfile
use File::Basename;
require './dftcfg-lib.pl';
&ReadParse();
&header($text{'bk_title'},undef,"index",undef,0);
$date = `date +%Y%m%d%H%M%S`;
$date =~ /^(\d+)/;
$date = $1;
$tmpname = "/sbin/webmin/dftcfg/cfg";
$tmpnamedate = $tmpname.$date;
$source = "/sysvol/etc";
$out = `tar cfvz $tmpnamedate $source`;
rename($tmpnamedate, $tmpnamedate.".gz");
my $filename=$tmpnamedate.".gz";
my $dir=dirname($filename);
my $base=basename($filename);
opendir(DIR,"$dir");
my @list=grep(/^$base/,readdir(DIR));
closedir(DIR);

print <<EOF;
<table class=mode1 border=0 width=100% align=center cellspacing=0 cellpadding=0>
<tr class=title1>
<td><b>$text{'cm_filename'}</b></td>
<td><b>$text{'cm_size'}</b></td>
<td><b>$text{'cm_date'}</b></td>
</tr>
EOF

foreach $filename (@list) {
	my (@st,$ctime,$downfile);

	@st=stat("$dir/$filename");
#	next unless( $st[7] >0 );

	if( $filename !~ /\.gz/){ 
		$downfile=$filename;
		$type='binary';
	} 
	else {
		$downfile=$filename;
		$ctime=scalar localtime($st[10]);
		$type='binary';
	}

#	unlink($downfile) if(-e $downfile);
#	symlink("$dir/$filename",$downfile);
	$mode=$mode eq 'mode1' ? 'mode2':'mode1';

print <<EOF;
<tr class=$mode>
<td>
    <a href=$downfile><img border=0 src=../images/$type.gif></a>&nbsp;$downfile 
</td>
<td>$st[7]</td>
<td>$ctime</td>
</tr>
EOF

}
print "<tr><td colspan=3><hr></td></tr>";
print "<tr><td>$text{'bknote'}</td></tr>";
print "</table>";
&footer("index.cgi", $text{'index'});
